home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / OpenLinux 2.3 CD.iso / live / usr / lib / rpm-2.5.5 / mklists.pl < prev    next >
Encoding:
Perl Script  |  1998-07-29  |  4.4 KB  |  176 lines

  1. #!/usr/bin/perl -w
  2. # $Id: mklists.pl,v 1.3 1998/07/29 12:39:11 ray Exp $
  3. #use strict;
  4.  
  5. my $C = $0; $C =~ s%.*/%%;
  6.  
  7. my $Mode = "create";
  8. my $Append = "";
  9. my $Help = 0;
  10. my $OptErr = "";
  11. my $debug = 0;
  12. local $missed = "MISSED";
  13.  
  14. my %Default =
  15.    ( 
  16.     "dirs",
  17.     [[ "^/(bin|dev|etc|lib|sbin|usr|var)/\$",        "IGNORED"],
  18.      [ "^/usr/(bin|doc|etc|games|include|info)/\$",    "IGNORED"],
  19.      [ "^/usr/(lib|man|sbin|share|src)/\$",        "IGNORED"],
  20.      [ "^/usr/doc/",                    "IGNORED"],
  21.      [ "^/usr/man/man[123456789n]/\$",            "IGNORED"],
  22.      [ "^/var/(lib|lock|log|run|spool|state|tmp)/\$",    "IGNORED"],
  23.      [ ".*",                        "base"],
  24.     ],
  25.     "files",
  26.     [[ "/man/man[1456789n]/",                "base"],
  27.      [ "/man/man[23]/",                 "devel"],
  28.      [ "^/usr/include",                 "devel"],
  29.      [ "^(/usr)?/lib/.*\.so\$",             "devel"],
  30.      [ "^(/usr)?/lib/.*\.a\$",                "devel-static"],
  31.      [ "^\s*\$",                    "IGNORED"],
  32.      [ "^\#",                        "IGNORED"],
  33.      [ ".*",                        "base"],
  34.     ]
  35. );
  36.  
  37. while ( $#ARGV >= $[ && ($_ = shift, /^-/ || (unshift(@ARGV,$_) && 0)) ) {
  38.   last if /^--$/;
  39.   (/^--create$/ || /^-c$/)        && ($Mode = "create", next);
  40.   (/^--dirs$/ || /^-d$/)        && ($Mode = "dirs", next);
  41.   (/^--files$/ || /^-f$/)        && ($Mode = "files", next);
  42.   (/^--append$/ || /^-a$/)        && ($Append = ">", next);
  43.   (/^--debug$/ || /^-D$/)        && ($debug ++, next);
  44.   (/^--?help/ || /^-h/)            && ($Help = 1, next);
  45.   (/^-/)                && ($OptErr .= "$_ ", next);
  46. }
  47.  
  48. if ( $OptErr ) {
  49.   printf( STDERR "$C: unkown option: $OptErr\n");
  50. }
  51.  
  52. my $Pkg = shift;
  53.  
  54.  
  55. if ( $OptErr || $Help ) {
  56.   die( "Usage: $C [-acdfh] pkg-name\n");
  57. }
  58.  
  59. if ( "$Mode" eq "create" ) {
  60.   my $D = $ENV{DESTDIR} || die( "$C: DESTDIR: no variable\n");
  61.   system( "rm -f dirs-$Pkg files-$Pkg files-$Pkg-*" ) &&
  62.      die( "$C: removing: $!\n");
  63.   system( "find $D -type d -mindepth 1 -printf '/%P/\n' | sort > dirs-$Pkg") &&
  64.      die( "$C: find dirs: $!\n");
  65.   system( "find $D -not -type d -printf '/%P\n' | sort > files-$Pkg") &&
  66.      die( "$C: find files: $!\n");
  67.   exit( 0);
  68. }
  69.  
  70. if ( ! -r "$Mode-$Pkg" ) {
  71.   die( "$C: $Mode-$Pkg: $!\n");
  72. }
  73.  
  74. my $i;
  75. local @f2p = compilePattern($Mode, @{$Default{$Mode}});
  76. my @subs = listSubs( @f2p);
  77.  
  78. open( IN, "< $Mode-$Pkg" ) || die( "open('$Pkg'): $!\n");
  79. foreach $i ( @subs ) {
  80.   printf( STDERR "open: >$Append files-$Pkg-$i\n") if ( $debug >= 2 );
  81.   open( $i, ">$Append files-$Pkg-$i") || die( "open('$Pkg-$i'): $!\n");
  82. }
  83.  
  84. while ( <IN> ) {
  85.   my $out = match( $_);
  86.   printf( STDERR "%-20s %s", "$out:", $_) if ( $debug >= 6 );
  87.   print( $out $Prefix . $_);
  88. }
  89. close( IN);
  90.  
  91. foreach $i ( @subs ) {
  92.   close( $i);
  93.   if ( -z "files-$Pkg-$i" ) {
  94.     printf( STDERR "removing empty '%s'\n", "files-$Pkg-$i")
  95.        if ( $debug >= 1 );
  96.     unlink( "files-$Pkg-$i");
  97.   }
  98. }
  99.  
  100. if ( -r "files-$Pkg-$missed" ) {
  101.   printf( STDERR "$C: non-empty safety net: files-$Pkg-$missed\n");
  102.   exit( 1);
  103. }
  104.  
  105. exit( 0);
  106.  
  107. sub compilePattern($@) {
  108.   my( $mode, @default) = @_;
  109.   my( @p2p) = ();
  110.  
  111.   if ( $Mode eq "dirs" ) {
  112.     $Prefix = "%dir ";
  113.   } else {
  114.     $Prefix = "";
  115.   }
  116.  
  117.   unshift(@ARGV, '-') if $#ARGV < $[;
  118.   while ( <> ) {
  119.     next if ( m:^\s*$: || m:^\s*\#: );
  120.     print( STDERR "processing") if ( $debug );
  121.     if ( m:^\@default: ) {
  122.       print( STDERR " defaults ") if ( $debug );
  123.       for ( $i = 0 ; $i <= $#default ; $i ++ ) {
  124.     print( STDERR ".") if ( $debug );
  125.     push( @p2p, [ @{$default[$i]} ]);
  126.       }
  127.       print( STDERR "\n") if ( $debug );
  128.     } else {
  129.       print( STDERR " rule...\n") if ( $debug );
  130.       push( @p2p, [ split ]);
  131.     }    
  132.   }
  133.   # catch the rest...
  134.   push( @p2p,     [ ".*", $missed]);
  135.  
  136.   return ( @p2p );
  137. }
  138.  
  139. sub listSubs(@) {
  140.   my( @f ) = @_;
  141.   my( %s) = ();
  142.   my( $i, $j) = ( "", "");
  143.   my( @b) = ();
  144.  
  145.   for $i ( 0 .. $#f ) {
  146.     printf( STDERR "pkg='%s' pattern='%s'\n", $f[$i][1], $f[$i][0])
  147.        if ( $debug );
  148.     $s{$f[$i][1]} ++;
  149.   }
  150.  
  151.   foreach $i ( sort( keys( %s)) ) {
  152.     printf( STDERR "sub='%s': %d\n", $i, $s{$i}) if ( $debug >= 2 );
  153.     next unless ( defined( $s{$i}) && $s{$i} > 0 );
  154.     push( @b, $i) unless ( $i eq $j );
  155.     $j = $i;
  156.   }
  157.   printf( STDERR "subs: '%s'\n", join( ', ', @b)) if ( $debug >= 1 );
  158.   return ( @b );
  159. }
  160.  
  161. sub match($) {
  162.   my ( $t) = @_;
  163.   my ( $i) = 0;
  164.  
  165.   while ( $i <= $#f2p ) {
  166.     my $p = $f2p[$i][0];
  167.     printf( STDERR "testing(%d): '%s'\n", $i, $p) if ( $debug >= 99 );
  168.     last if ( $t =~ m:$p: );
  169.     $i ++;
  170.   }
  171.   printf( STDERR "Ouch: undefined: \$iff2p[$i]\n") 
  172.      unless ( defined( $f2p[$i]) );
  173.   return ( (defined( $f2p[$i])) ? $f2p[$i][1] : $missed );
  174. }
  175.  
  176.